<?php
/**
 * 共享商品
 *
 * @author Zeon <scplay@gmail.com>
 * @date 2018/9/11 13:46
 * @copyright PanxSoft Inc.
 */

namespace App\Http\Controllers\Admin\Customer\Share;


use App\Http\Controllers\ApiController;
use App\Http\Requests\Admin\Customer\Product\ShareProductIndexRequest;
use App\Http\Requests\Admin\Customer\Product\ShareProductStoreRequest;
use App\Models\Admin\CustomerAdmin;
use App\Models\Customer\Customer;
use App\Models\Customer\Language;
use App\Models\Panx\Category;
use App\Models\Panx\Product;
use App\Services\Product\CopyProductService;
use App\Services\Repositories\Product\ShareProductRepository;
use App\Services\Utils\Guard;
use App\Services\Utils\Transaction;
use Illuminate\Pagination\LengthAwarePaginator;

class ShareProductController extends ApiController
{
    const ROUTES = [
        'index' => 'api.admin.customer.share-product.index',
        'store' => 'api.admin.customer.share-product.store',
    ];

    const DEFAULT_ORDER_BYS = [
        ['order_by' => 'ref_count', 'sort' => 'desc']
    ];

    /**
     * 客户后台，可选共享商品列表
     * @test
     *
     * @param ShareProductIndexRequest $request
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function index(ShareProductIndexRequest $request)
    {
        $this->checkCustomer();

        // 超管才能操作的
        $this->checkIsTrue(CustomerAdmin::isGuardSuperAdmin(), trans('admin.is_super_man'));

        /** @var Customer $customer */
        $customer = Guard::customer();

        $customer_categories = $customer->categoriesIsShowLatestPriority();

        $providers = Customer::virtualProviders();

        $has_asset = $request->product_type ?? Category::CUSTOM_TYPE_COSMETICS ;

        /** @var LengthAwarePaginator $products */
        $products = (new ShareProductRepository)->indexShareProducts(
            $providers->modelKeys(), $customer_categories->modelKeys(),
            $has_asset, $customer->id, $request
        );


        return $this->response(compact('products'));
    }

    /**
     * @param ShareProductRepository $share_product_repository
     * @return array|\Illuminate\Http\Response
     */
    public function create(ShareProductRepository $share_product_repository)
    {
        /** @var CustomerAdmin $customer_admin */
        $customer_admin = Guard::customerAdmin();
        /** @var Customer $customer */
        $customer = $customer_admin->customer;
        $language_id = Language::mapCodeToId($customer_admin->language);

        $result = $share_product_repository->getBrandsAndGroupCategories($customer, $language_id);

        return $this->response($result);
    }

    /**
     * 客户后台，复制共享商品
     * @test
     *
     * @param ShareProductStoreRequest $request
     * @param CopyProductService $copyProductService
     *
     * @throws \Exception
     * @return array|\Illuminate\Http\Response
     */
    public function store(ShareProductStoreRequest $request,
                          CopyProductService $copyProductService)
    {
        $this->checkCustomer();

        // 超管才能操作的
        $this->checkIsTrue(CustomerAdmin::isGuardSuperAdmin(), trans('admin.is_super_man'));

        $prod_id = $request->product_id;
        /** @var Product $old_prod */
        $old_prod = (new Product)->find($prod_id);
            $this->checkItemExist($old_prod);

        $old_prod->new_brand_id = $request->brand_id;
        $old_prod->new_brand_series_id = $request->brand_series_id;

        $customer = Guard::customer();
            $copyProductService->checkCustomerHasCategories([$old_prod->category_id], $customer);

        $new_product = Transaction::handle(function () use ($copyProductService, $old_prod, $customer) {
            return $copyProductService->toCustomer($old_prod, $customer);
        });

        return $this->response(compact('new_product'));
    }
}